package com.longai.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import util.Page;

import com.google.gson.Gson;
import com.longai.model.Momentlike;
import com.longai.model.Picture;
import com.longai.model.UploadFileInfo;
import com.longai.model.User;
import com.longai.service.FileUpload;
import com.longai.service.PictureService;
import com.longai.service.UserService;
import com.longai.vo.MomentLikeVo;
import com.longai.vo.PictureVo;

import functions.GetProperties;
import functions.Px;

@Controller
@RequestMapping("picture")
public class PictureAction {

	@Autowired
	PictureService pictureService;
	FileUpload fileupload;
	@Autowired
	UserService userService;

	//上传图片 需要修改判断在数据库中是否存在user_id，然后添加进去
	@RequestMapping(value = "picUpload",method=RequestMethod.POST)
	@ResponseBody
	public String handleFileUpload(@RequestParam("picUpload") MultipartFile file,@RequestParam("picDetail")String picDetail,
			HttpServletRequest request) throws IOException {
		System.out.println("有没有");
		try {
			UploadFileInfo uploadFileInfo = fileupload.upload(file, "picUpload",
					request,58,58);
			// 需要获取的参数
			Gson gson = new Gson();
			PictureVo pictureDetail = gson.fromJson(picDetail, PictureVo.class);
			if (uploadFileInfo.isStatus()) {
				Picture picture = new Picture();
				picture.setPicturePubtime(uploadFileInfo.getTs());
				Integer userId = pictureDetail.getUserId();
				User user = userService.findById(userId);
				picture.setPicname(pictureDetail.getPicname());
				picture.setNailurl(uploadFileInfo.getName());
				picture.setOriginallurl(uploadFileInfo.getName());
				picture.setPermisson(pictureDetail.getPermisson());
				picture.setUser(user);
				pictureService.save(picture);
				System.out.println("success");
				return "1";
			} else {
				System.out.println("上传失败");
				return "0";
			}
			
		} catch (Exception e) {
			System.out.println(e);
			return "0";
		}

		
		
	}
	
	//author by miao
	//删除图片
	//参数picId图片Id
	//成功返回"1",失败返回"0"
	@RequestMapping(value = "deletePic",method=RequestMethod.GET)
	@ResponseBody
	public String deletePic(@RequestParam("picId")String picId){
		
		try {
		    Picture picture = pictureService.findById(Integer.parseInt(picId));
		    pictureService.delete(picture);
		    return "1";
		} catch (Exception e) {
			System.out.println("删除失败");
			return "0";
		}
	}
	
	
	//获取图片信息
	//
	//成功返回picture对象，失败返回""
	@RequestMapping(value = "getPic",method=RequestMethod.GET)
	@ResponseBody
	public Object getPic(@RequestParam("picId")String picId){
		
		try {
			Picture picture = pictureService.findById(Integer.parseInt(picId));
			PictureVo pictureVo = new PictureVo(picture);
			return pictureVo;
		} catch (Exception e) {
			System.out.println(e);
			return "";
		}  		   
	}
	
	//获取相册总信息
	@RequestMapping(value = "getTotalPic",method=RequestMethod.GET)
	@ResponseBody
	public Object getTotalPic(@RequestParam("userId")String userId,@RequestParam("currentPage")String currentPage,@RequestParam("amount")String amount){
		
		try {
			Page page = pictureService.findByUserId(userId, currentPage, amount);	
			List<Picture> list = page.getList();
			List<PictureVo> list2 = new ArrayList<PictureVo>();
			for (Picture img : list) {
				img.setNailurl(Px.px("nail")+img.getNailurl());
				img.setOriginallurl(Px.px("picUpload")+img.getOriginallurl());
				img.getUser().setProfilepic(Px.px("userIcon")+img.getUser().getProfilepic());
				PictureVo pictureVo = new PictureVo(img);
				list2.add(pictureVo);
			}
			page.setList(list2);
			return page;
		} catch (Exception e) {
			System.out.println(e);
			return "";
		}
	}
}
